package com.company.geometric.ui;

import com.company.geometric.background.*;
import com.company.geometric.background.Point;
import sun.dc.pr.PRError;

import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.LinkedList;

/**
 * Created by v on 16-11-30.
 */
public class MainUI extends JFrame{
    private ControlPanel controlPanel=new ControlPanel();
    private MyPanel panel=new MyPanel();

    public MainUI(){
        setLayout(new BorderLayout());
        add(controlPanel, BorderLayout.EAST);
        add(panel,BorderLayout.CENTER);
    }

    public static void main(String[] args) {
        MainUI ui=new MainUI();
        ui.setVisible(true);
        ui.setLocation(300,100);
        ui.setSize(800,500);
        ui.setTitle("欢迎光临，几何绘图");
        ui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    /**
     * 作图类：
     */
    private class MyPanel extends JPanel{
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            LinkedList<Geometry> geometries=controlPanel.getGeometrys();

            for(int i=0;i<geometries.size();i++){
                if (i==controlPanel.target){
                    g.setColor(Color.RED);
                }
                if(geometries.get(i).getClass()==Circle.class){
                    Circle circle=(Circle)geometries.get(i);

                    g.drawOval(circle.getPoint().getX(),
                            circle.getPoint().getY(),
                            circle.getX(),
                            circle.getY());
                }else {
                    Polygone polygone=(Polygone)geometries.get(i);

                    com.company.geometric.background.Point[] points=polygone.getPoints();
                    for(int j=0;j<points.length-1;j++){
                        g.drawLine(points[j].getX(),
                                points[j].getY(),
                                points[j+1].getX(),
                                points[j+1].getY());
                    }

                    g.drawLine(points[points.length-1].getX(),
                            points[points.length-1].getY(),
                            points[0].getX(),
                            points[0].getY());
                }
                g.setColor(Color.BLACK);
            }
        }
    }


    /**
     * 内部类：控制面板
     */
    public class ControlPanel extends JPanel{
        private ListPanel control=new ListPanel();
        private JButton jbtSubtract=new JButton("-");
        private JButton jbtAdd=new JButton("+");

        //用于标记图形的编号
        private int number=0;

        //在滚动条处被选择的目标
        private int target=-1;

        //对话窗体
        private CircleDialog circleDialog=new CircleDialog();
        private PolygoneDialog triangleDialog=new PolygoneDialog("三角形",3);
        private PolygoneDialog suqareDialog=new PolygoneDialog("四边形",4);
        private PolygoneDialog pentagonDialog =new PolygoneDialog("五边形",5);

        //控制面板
        public ControlPanel(){
            JPanel panel=new JPanel();
            panel.setLayout(new GridLayout(1,2));
            panel.add(jbtAdd);
            panel.add(jbtSubtract);
            jbtAdd.setFont(new Font("微软雅黑",0,20));
            jbtSubtract.setFont(new Font("微软雅黑",0,20));

            jbtAdd.addActionListener(new ControlPanel.addLinkList());

            jbtSubtract.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    delete(target);
                }
            });

            setLayout(new BorderLayout(0,0));
            this.add(panel,BorderLayout.NORTH);
            this.add(control,BorderLayout.CENTER);
        }

        //滚动Panel
        private class ListPanel extends JScrollPane{
            private JList<Geometry> myList;
            private LinkedList<Geometry> geometries;
            private ListModel<Geometry> listModel;

            public ListPanel(){
                geometries=new LinkedList<>();
                listModel=new DefaultComboBoxModel(geometries.toArray());
                myList=new JList<>();
                myList.setFont(new Font("微软雅黑",0,20));
                myList.setModel(listModel);
                this.setViewportView(myList);

                //多选框监听器，对选中单位放入到target
                myList.addListSelectionListener(new ListSelectionListener() {
                    @Override
                    public void valueChanged(ListSelectionEvent e) {
                        target=myList.getSelectedIndex();
                        panel.repaint();
                    }
                });
            }

            public void rebuild(){
                listModel=new DefaultComboBoxModel(geometries.toArray());
                myList.setModel(listModel);
            }
        }

        //增加一个几何体
        public void add(Geometry geometry){
            control.geometries.add(geometry);
            control.rebuild();
            panel.repaint();
        }

        //拿到这些几何体
        public LinkedList<Geometry> getGeometrys(){
            return this.control.geometries;
        }

        //移除一个几何体
        public void delete(int index){
            control.geometries.remove(index);
            control.rebuild();
            panel.repaint();
        }

        //增加这个动作的监听器
        public class addLinkList implements ActionListener {
            @Override
            public void actionPerformed(ActionEvent e) {
                int choose=JOptionPane.showOptionDialog(
                        null,
                        "请选择你要绘制的图案",
                        "选择图案",
                        JOptionPane.DEFAULT_OPTION,
                        JOptionPane.PLAIN_MESSAGE,
                        null,
                        new Object[]{"圆形","三角形","四边形",
                                "五边形"},
                        "圆形"
                );

                switch (choose){
                    case 0:
                        circleDialog.setVisible(true);break;
                    case 1:
                        triangleDialog.setVisible(true);break;
                    case 2:
                        suqareDialog.setVisible(true);break;
                    case 3:
                        pentagonDialog.setVisible(true);break;
                }
            }
        }

        //圆形的UI
        public class CircleDialog extends JDialog{
            private JLabel centerX=new JLabel("圆心x：");
            private JLabel centerY=new JLabel("圆心y：");
            private JLabel radius=new JLabel("半径：");
            private JTextField field1=new JTextField(20);
            private JTextField field2=new JTextField(20);
            private JTextField field3=new JTextField(20);

            public CircleDialog(){
                super((java.awt.Dialog)null,true);

                setSize(300,100);

                setLocationRelativeTo(null);

                JPanel panel=new JPanel();
                panel.setLayout(new GridLayout(3,3));
                panel.add(centerX);
                panel.add(field1);
                panel.add(centerY);
                panel.add(field2);
                panel.add(radius);
                panel.add(field3);
                add(panel,BorderLayout.CENTER);

                JButton jbtOk=new JButton("OK");
                JButton jbtCancel=new JButton("Cancel");

                jbtOk.addActionListener(new AbstractAction() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        try{
                            Circle circle=new Circle(
                                    Integer.valueOf(field1.getText()),
                                    Integer.valueOf(field2.getText()),
                                    Integer.valueOf(field3.getText()),
                                    "圆"+number++
                            );

                            ControlPanel.this.add(circle);
                        }catch (Exception ex){
                            JOptionPane.showMessageDialog(null,"您的输入有误");
                        }finally {
                            setVisible(false);
                        }
                    }
                });

                jbtCancel.addActionListener(new AbstractAction() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        setVisible(false);
                    }
                });

                JPanel panel2=new JPanel();
                panel2.add(jbtOk);
                panel2.add(jbtCancel);
                add(panel2,BorderLayout.SOUTH);
            }
        }

        //多边形的UI
        public class PolygoneDialog extends JDialog{
            private JLabel[] labels;
            private JTextField[] textFields;

            public PolygoneDialog(String name, int sides){
                super((java.awt.Dialog)null,true);
                labels=new JLabel[sides*2];
                textFields=new JTextField[sides*2];

                for(int i=0;i<labels.length;i=i+2){
                    labels[i]=new JLabel("点"+(i/2+1)+"x: ");
                    labels[i+1]=new JLabel("点"+(i/2+1)+"y: ");
                }

                for(int i=0;i<textFields.length;i++){
                    textFields[i]=new JTextField(20);
                }

                setSize(300,sides*30);

                setLocationRelativeTo(null);

                JPanel panel=new JPanel();
                panel.setLayout(new GridLayout(sides,4));
                for(int i=0;i<labels.length;i++){
                    panel.add(labels[i]);
                    panel.add(textFields[i]);
                }
                add(panel,BorderLayout.CENTER);

                JButton jbtOk=new JButton("OK");
                JButton jbtCancel=new JButton("Cancel");

                jbtOk.addActionListener(new AbstractAction() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        try{
                            int[] x=new int[sides];
                            int[] y=new int[sides];

                            for(int i=0;i<2*sides;i=i+2){
                                x[i/2]=Integer.valueOf(textFields[i].getText());
                                y[i/2]=Integer.valueOf(textFields[i+1].getText());
                            }
                            Polygone polygone;

                            switch (sides){
                                case 3:
                                    polygone=new Polygone(
                                            "三角形"+number++,sides,x,y);
                                    break;
                                case 4:
                                    polygone=new Polygone(
                                            "四边形"+number++,sides,x,y);
                                    break;
                                default:
                                    polygone=new Polygone(
                                            "五边形"+number++,sides,x,y);
                                    break;
                            }

                            ControlPanel.this.add(polygone);
                        }catch (Exception ex){
                            JOptionPane.showMessageDialog(null,"您的输入有误");
                        }finally {
                            setVisible(false);
                        }
                    }
                });

                jbtCancel.addActionListener(new AbstractAction() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        setVisible(false);
                    }
                });

                JPanel panel2=new JPanel();
                panel2.add(jbtOk);
                panel2.add(jbtCancel);
                add(panel2,BorderLayout.SOUTH);
            }
        }
    }
}